(function($){
	function init(container) {
		var state = $.data(container, 'jqpagination');
		if(state.options.pageSize && state.options.pageSize != 0) {
			state.options.pageTotal = parseInt(state.options.total / state.options.pageSize) + (state.options.total % state.options.pageSize == 0 ? 0 : 1);
			if(state.options.pageTotal == 0)state.options.pageTotal++;
		}
		if(state.options.pageTotal < state.options.pageCurrent) state.options.pageCurrent = state.options.pageTotal;
		if(state.options.automatic) state.options.pageCount = automatic(container,state.options.pageCount);
		createPage(container,state.options);
	}
	//自动计算分页页码方块个数
	function automatic(container,oldpageCount){
		var width = $(container).width(),
			item_w = $(window).width() > 1500 ? 40 : 30,
		pageCount = parseInt(width / item_w) - 6;
		if(pageCount < 1) pageCount = 1;
		return pageCount > oldpageCount ? oldpageCount : pageCount;
	}
	
	//创建分页页码方块
	function createPage(container,options){
		var list = document.createElement("div");
		list.className = "jquery-pagination-list";
		
		var prev = document.createElement("div");
		prev.className = "jquery-pagination-item jquery-pagination-icon" + (options.pageCurrent == 1 ? ' disabled' : '');
		list.appendChild(prev);
		var prev_i = document.createElement("i");
		prev_i.className = "fa fa-angle-left";
		prev.appendChild(prev_i);
		if(options.pageCurrent != 1) {
			addEvent(prev,"click",function(){
				options.pageCurrent--;
				createPage(container,options);
			});
		}
		
		var item_first = document.createElement("div");
		item_first.className = "jquery-pagination-item" + (options.pageCurrent == 1 ? ' active' : '');
		item_first.innerHTML = 1;
		list.appendChild(item_first);
		
		addEvent(item_first,"click",function(){
			options.pageCurrent = 1;
			createPage(container,options);
		});
		
		if(options.pageTotal > 1){
			var start = options.pageCurrent - parseInt(options.pageCount / 2);
			if(start <= 1) 
				start = 2; 
			else {
				var item_prevs = document.createElement("div");
				item_prevs.className = "jquery-pagination-item jquery-pagination-icon prev";
				list.appendChild(item_prevs);
				
				var ellipsis = document.createElement("i");
				ellipsis.className = "fa fa-ellipsis-h";
				item_prevs.appendChild(ellipsis);
				
				var prevs_i = document.createElement("i");
				prevs_i.className = "fa fa-angle-double-left";
				item_prevs.appendChild(prevs_i);
				addEvent(item_prevs,"click",function(){
					options.pageCurrent = options.pageCurrent - options.pageCount;
					if(options.pageCurrent < 1) options.pageCurrent = 1;
					createPage(container,options);
				});
			};
			for(var i = start; i < options.pageTotal; i++){
				if(i >= start + options.pageCount){
					break;
				}
				var item = document.createElement("div");
				item.className = "jquery-pagination-item" + (options.pageCurrent == i ? ' active' : '');
				item.innerHTML = i;
				list.appendChild(item);
				addEvent(item,"click",function(e){
					options.pageCurrent = parseInt(e.target.innerText);
					createPage(container,options);
				});
			}
			if(start + parseInt(options.pageCount / 2) < options.pageTotal - 1){
				var item_nexts = document.createElement("div");
				item_nexts.className = "jquery-pagination-item jquery-pagination-icon next";
				list.appendChild(item_nexts);
				
				var ellipsis = document.createElement("i");
				ellipsis.className = "fa fa-ellipsis-h";
				item_nexts.appendChild(ellipsis);
				
				var nexts_i = document.createElement("i");
				nexts_i.className = "fa fa-angle-double-right";
				item_nexts.appendChild(nexts_i);
				
				addEvent(item_nexts,"click",function(){
					options.pageCurrent = options.pageCurrent + options.pageCount;
					if(options.pageCurrent > options.pageTotal) options.pageCurrent = options.pageTotal;
					createPage(container,options);
				});
			}
			var item_last = document.createElement("div");
			item_last.className = "jquery-pagination-item" + (options.pageCurrent == options.pageTotal ? ' active' : '');
			item_last.innerHTML = options.pageTotal;
			list.appendChild(item_last);
			addEvent(item_last,"click",function(){
				options.pageCurrent = options.pageTotal;
				createPage(container,options);
			});
		}
		
		var next = document.createElement("div");
		next.className = "jquery-pagination-item jquery-pagination-icon" + (options.pageCurrent == options.pageTotal ? ' disabled' : '');
		list.appendChild(next);
		var next_i = document.createElement("i");
		next_i.className = "fa fa-angle-right";
		next.appendChild(next_i);
		if(options.pageCurrent != options.pageTotal) {
			addEvent(next,"click",function(){
				options.pageCurrent++;
				createPage(container,options);
			});
		}
		
		$(container).html(list);
		options.onChange(options.pageCurrent);
	}
	//绑定事件
	function addEvent(obj, type, handle) {
		try { // Chrome、FireFox、Opera、Safari、IE9.0及其以上版本
			obj.addEventListener(type, handle, false);
		} catch (e) {
			try { // IE8.0及其以下版本
				obj.attachEvent('on' + type, handle);
			} catch (e) { // 早期浏览器
				obj['on' + type] = handle;
			}
		}
	}
	
	$.fn.jqpagination = function(options, param) {
		if (typeof options == 'string') {
			return $.fn.jqpagination.methods[options](this, param);
		}
		options = options || {};
		return this.each(function() {
			var state = $.data(this, 'jqpagination');
			if (state) {
				$.extend(state.options, options);
			} else {
				$.data(this, 'jqpagination', {
					options: $.extend({}, $.fn.jqpagination.defaults, options),
					jqpagination: $(this).addClass('jquery-pagination')
				});
				init(this);
			}
		});
	}
	$.fn.jqpagination.methods = {
		getCurrent: function(jq){
			return $.data(jq[0], "jqpagination").options.pageCurrent;
		},
		setOptions: function(jq,options){
			$.extend($.data(jq[0], "jqpagination").options,options);
			init(jq[0]);
		},
	},
	$.fn.jqpagination.defaults = {
		automatic: false,
		pageCurrent: 1,
		pageSize: 20,
		total: 0,
		pageTotal: 1,
		pageCount: 5,
		onChange: function(pageCurrent){
			
		},
	}
})(jQuery)